package com.das.mechanic_alone.mvp.presenter.alone;

import com.das.mechanic_alone.mvp.contract.alone.X3AloneServiceContract;
import com.das.mechanic_base.base.X3BasePresenter;
import com.das.mechanic_base.bean.alone.SearchBean;
import com.das.mechanic_base.bean.alone.ServiceHeaderBean;
import com.das.mechanic_base.bean.greendao.AloneServiceListBean;
import com.das.mechanic_base.bean.greendao.AloneServiceNewBean;
import com.das.mechanic_base.mapi.api.NetWorkHttp;
import com.das.mechanic_base.mapi.response.HttpCallBack;
import com.das.mechanic_base.mapi.schedulers.RxSchedulersHelper;
import com.das.mechanic_base.utils.X3AloneLayoutUtils;
import com.das.mechanic_base.utils.X3StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class X3AloneServicePresenter extends X3BasePresenter<X3AloneServiceContract.View> implements X3AloneServiceContract.Presenter{

    @Override
    public void getServiceBaseInfo(Map<String,Object> map,boolean isJump) {
        NetWorkHttp.getApi().showServiceBaseListInfo(map)
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .compose(mView.bindToLife())
                .subscribe(new HttpCallBack<List<ServiceHeaderBean>>() {
                    @Override
                    protected void onDone(List<ServiceHeaderBean> serviceHeaderBeans) {
                        mView.getServiceInfo(serviceHeaderBeans,isJump);
                    }

                    @Override
                    protected String LoadingMessage() {
                        return null;
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });
    }

    @Override
    public void getDetectionServiceList(boolean isStart) {
        NetWorkHttp.getApi().getTouchServiceList(mView.providerWorkBaseId())
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .compose(mView.bindToLife())
                .subscribe(new HttpCallBack<List<AloneServiceNewBean>>() {
                    @Override
                    protected void onDone(List<AloneServiceNewBean> mList) {
                        if (X3StringUtils.isListEmpty(mList)) {
                            mView.getServiceList(new ArrayList<>(),isStart);
                            return;
                        }

                        mView.getServiceList(X3AloneLayoutUtils.getCurrentServiceList(mList),isStart);
                    }

                    @Override
                    protected String LoadingMessage() {
                        return null;
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });
    }

    @Override
    public void saveDetectionService(List<AloneServiceListBean> mList) {
        if (X3StringUtils.isListEmpty(mList)) {
            mList = new ArrayList<>();
        }
        List<AloneServiceListBean> finalMList = mList;
        NetWorkHttp.getApi().saveDetectionServiceList(mList,mView.providerWorkBaseId())
                .compose(mView.bindToLife())
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .subscribe(new HttpCallBack<Object>() {
                    @Override
                    protected void onDone(Object o) {
                        mView.getUpdateServiceList(finalMList);
                    }

                    @Override
                    protected String LoadingMessage() {
                        return null;
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });
    }

    @Override
    public void listServiceBaseByCategory(SearchBean searchBean) {
        NetWorkHttp.getApi().listServiceBaseByCategory(searchBean)
                .compose(mView.bindToLife())
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .subscribe(new HttpCallBack<List<ServiceHeaderBean.ShopCategoryEntityBean.ServiceBaseEntityBean>>() {
                    @Override
                    protected void onDone(List<ServiceHeaderBean.ShopCategoryEntityBean.ServiceBaseEntityBean> list) {
                        mView.listServiceBaseByCategorySus(list);
                    }

                    @Override
                    protected String LoadingMessage() {
                        return null;
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });
    }
}
